package com.example.androidsqlitecontactdemo2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener {
    // 申明控件成员变量
    EditText mEtName,mEtTel;
    Button mBtnQueryAll,mBtnAdd,mBtnDel,mBtnUpdate, mBtnQueryByName;
    TextView mTvDataMsg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();// 为控件绑定监听器对象
    }

    private void initEvent() {
        mBtnAdd.setOnClickListener(this);
        mBtnUpdate.setOnClickListener(this);
        mBtnDel.setOnClickListener(this);
        mBtnQueryAll.setOnClickListener(this);
        mBtnQueryByName.setOnClickListener(this);
    }

    private void initView() {
        mEtName = findViewById(R.id.et_name);
        mEtTel = findViewById(R.id.et_tel);
        mBtnAdd = findViewById(R.id.btn_add);
        mBtnQueryAll = findViewById(R.id.btn_query_all);
        mBtnDel = findViewById(R.id.btn_del);
        mBtnUpdate = findViewById(R.id.btn_update);
        mBtnQueryByName = findViewById(R.id.btn_query_by_name);
        mTvDataMsg = findViewById(R.id.tv_data_msg);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.btn_add:
                // 添加数据
                Add();
                showAll();// 更新界面显示
                Toast.makeText(this, "R.id.btn_add", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_query_all:// 查询所有
                showAll();
                Toast.makeText(this, " R.id.btn_query_all", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_del:
                delete();
                showAll();
                Toast.makeText(this, " btn_del", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_query_by_name:
                queryByName();
                Toast.makeText(this, " R.id.btn_query_by_name", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_update:
                update();
                showAll();
                Toast.makeText(this, " R.id.btn_update", Toast.LENGTH_SHORT).show();
                break;
    }
}


    // 根据名字查询信息并更新界面
    private void queryByName() {
        String name = mEtName.getText().toString();

        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("contacttb", null, "name=?", new String[]{name}, null, null, null, "1");
        StringBuffer sb = new StringBuffer();
        if (cursor.moveToNext()){
            sb.append(cursor.getLong(0)+"   "+cursor.getString(1)+"   "+cursor.getString(2));
        }
        String msg = sb.toString();
        mTvDataMsg.setText(msg);
        cursor.close();
        db.close();
    }
    /**
     * 修改记录，修改电话号码，用户名不改
     */
    private void update() {
        // 获取用户数据
        String name = mEtName.getText().toString();
        String tel = mEtTel.getText().toString();
        //
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("tel",tel);

        db.update("contacttb" , values ,"name=?",new String[]{name});
        db.close();
    }



    private void delete() {
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        db.delete("contacttb","name=?",new String[]{ mEtName.getText().toString() });
        db.close();
    }


    private void Add() {
        MyHelper helper = new MyHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("name",mEtName.getText().toString());
        values.put("tel",mEtTel.getText().toString());

        db.insert("contacttb",null,values);
        db.close();
    }
    private void showAll() {
        StringBuffer sb = new StringBuffer();

        MyHelper helper = new MyHelper(MainActivity.this);
        SQLiteDatabase db = helper.getReadableDatabase();// 获取数据库操作对象db
        Cursor cursor = db.query("contacttb", null, null, null, null, null, null);
        while (cursor.moveToNext()){
            long _id = cursor.getLong(0);
            String name = cursor.getString(1);
            String tel = cursor.getString(2);
            sb.append(_id + "   "+ name+"   "+ tel+ "\n");
        }
        cursor.close();
        String msg = sb.toString();// msg 保存所有查询到的记录数据
        mTvDataMsg.setText(msg);
        db.close();
    }
}



